﻿package org.net{
    
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.LoaderContext;
	
	import org.abcFrame.core.*;
	import org.enmu.*;
	import mx.managers.CursorManager;
	import flash.system.ApplicationDomain;


	/**
	 * 文件加载类，支持jpeg,swf,jpg,gif,png及文本文件 
	 * @author Administrator
	 * 
	 */    
	public class AbcHTTPServices {
        
		/**
		 * 构造函数 
		 * @param path 文件路径
		 * @param responder 异步响应捕捉类
		 * @param value 要传递的参数，默认值为null
		 * 
		 */		
		public function AbcHTTPServices(path:String,responder:AbcResponder,value:XML=null):void {

			var words:Array=path.split("\.");
			switch (words[words.length-1]) {
				case "swf" :
				case "jpeg" :
				case "jpg" :
				case "gif" :
				case "png" :
					binaryCommand(path,responder);
					break;
				default:
					textCommand(path,responder,value);
					break;
			}

		}
		/**
		 * 加载文本方法 
		 * @param path 文件路径
		 * @param responder 异步响应捕捉类
		 * @param value 要传递的参数，默认值为null
		 * 
		 */        
		private function textCommand(path:String,responder:AbcResponder,value:XML=null):void {

			CursorManager.setBusyCursor();
			
			var loader:URLLoader = new URLLoader();
			loader.dataFormat=URLLoaderDataFormat.TEXT;
			configureListeners(loader);
			var request:URLRequest = new URLRequest(path);
			if(value!=null){
			var variables:URLVariables = new URLVariables();
			variables.data=value;
			request.data = variables;
			request.method = URLRequestMethod.POST;
			}
			try {
				loader.load(request);
			} catch (error:Error) {
				AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"loader:加载"+path+"出问题了===="+error.message)
				if(responder.onFault!=null)responder.onFault(error,path)
			}

			function configureListeners(dispatcher:IEventDispatcher):void {
				dispatcher.addEventListener(Event.COMPLETE, completeHandler);
				dispatcher.addEventListener(Event.OPEN, openHandler);
				dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
				dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
				dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
				dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			}

			function completeHandler(event:Event):void {
				//try {
					//trace(URLLoader(event.target).data)
					if(responder.onResult!=null)responder.onResult(URLLoader(event.target).data,path);
				//} catch (error:Error) {
					//AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"completeHandler:处理"+path+"返回结果的responder函数出错了,可能是参数或是逻辑错误！"+error.message)
					//if(responder.onFault!=null)responder.onFault(error,path)
				//}
				
				CursorManager.removeAllCursors()
			}

			function openHandler(event:Event):void {
				AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"openHandler:"+event.toString())
			}

			function progressHandler(event:ProgressEvent):void {
				if(responder.onProgress!=null)responder.onProgress(event.bytesLoaded/event.bytesTotal);
			}

			function securityErrorHandler(event:SecurityErrorEvent):void {
				AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"securityErrorHandler:加载"+path+"出问题了===="+event.text)
				CursorManager.removeAllCursors()
				if(responder.onFault!=null)responder.onFault(event,path)
			}

			function httpStatusHandler(event:HTTPStatusEvent):void {
				AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"httpStatusHandler:"+event.toString())
			}

			function ioErrorHandler(event:IOErrorEvent):void {
				AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"ioErrorHandler:加载"+path+"出问题了===="+event.text)
				CursorManager.removeAllCursors()    
				if(responder.onFault!=null)responder.onFault(event,path)
			}
		}
		/**
		 * 加载文件方法 
		 * @param path 文件路径
		 * @param responder 异步响应捕捉类
		 * 
		 */        
		private function binaryCommand(path:String,responder:AbcResponder) :void{

			CursorManager.setBusyCursor();
			
			var loader:Loader = new Loader();
			configureListeners(loader.contentLoaderInfo);
			var request:URLRequest=new URLRequest(path);
			
			var ldrContext:LoaderContext = new LoaderContext(true, ApplicationDomain.currentDomain);

			try {
				//跨域访问图片和swf文件时需要设置LoaderContext类属性checkPolicyFile为true
				loader.load(request,ldrContext);
			} catch (error:Error) {
				AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"loader:加载"+path+"出问题了===="+error.message)
				if(responder.onFault!=null)responder.onFault()
			}

			function configureListeners(dispatcher:IEventDispatcher):void {
				dispatcher.addEventListener(Event.COMPLETE, completeHandler);
				dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
				dispatcher.addEventListener(Event.INIT, initHandler);
				dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				dispatcher.addEventListener(Event.OPEN, openHandler);
				dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
				
			}

			function completeHandler(event:Event):void {
				
				try {
					if(responder.onResult!=null)responder.onResult(LoaderInfo(event.target).loader.content,path);
				} catch (error:Error) {
					AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"completeHandler:处理"+path+"返回结果的responder函数出错了,可能是参数或是逻辑错误！"+error.message)
					if(responder.onFault!=null)responder.onFault()
				}
				
				CursorManager.removeAllCursors()
				
			}

			function httpStatusHandler(event:HTTPStatusEvent):void {
				AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"httpStatusHandler:"+event.toString())
			}

			function initHandler(event:Event):void {
				AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"initHandler:"+event.toString())
			}

			function ioErrorHandler(event:IOErrorEvent):void {
				AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"ioErrorHandler:加载"+path+"出问题了===="+event.text)
				CursorManager.removeAllCursors()
				if(responder.onFault!=null)responder.onFault()
			}

			function openHandler(event:Event):void {
				AbcEventDispatcher.instance.dispatcher(AbcEventType.HTTPEVENT,"openHandler:"+event.toString())
			}

			function progressHandler(event:ProgressEvent):void {
				
				if(responder.onProgress!=null)responder.onProgress(event.bytesLoaded/event.bytesTotal);
			}

		}

	}

}